<template>
  <el-dialog
    :model-value="modelValue"
    @update:model-value="(value) => emit('update:modelValue', value)"
    :title="isEdit ? '编辑注册登记' : '新增注册登记'"
    width="800px"
    :close-on-click-modal="false"
  >
    <el-form
      ref="formRef"
      :model="form"
      :rules="rules"
      label-width="120px"
      size="default"
    >
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="登记编号" prop="registrationNo">
            <el-input v-model="form.registrationNo" placeholder="请输入登记编号" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="登记日期" prop="registrationDate">
            <el-date-picker
              v-model="form.registrationDate"
              type="date"
              placeholder="选择登记日期"
              style="width: 100%"
            />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="申请单位" prop="applicantUnit">
            <el-input v-model="form.applicantUnit" placeholder="请输入申请单位" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="联系人" prop="contactPerson">
            <el-input v-model="form.contactPerson" placeholder="请输入联系人" />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="联系电话" prop="contactPhone">
            <el-input v-model="form.contactPhone" placeholder="请输入联系电话" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="登记类型" prop="registrationType">
            <el-select v-model="form.registrationType" placeholder="选择登记类型" style="width: 100%">
              <el-option label="新建登记" value="new" />
              <el-option label="变更登记" value="change" />
              <el-option label="注销登记" value="cancel" />
              <el-option label="补办登记" value="reissue" />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="项目名称" prop="projectName">
            <el-input v-model="form.projectName" placeholder="请输入项目名称" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="项目类型" prop="projectType">
            <el-select v-model="form.projectType" placeholder="选择项目类型" style="width: 100%">
              <el-option label="水库工程" value="reservoir" />
              <el-option label="堤防工程" value="embankment" />
              <el-option label="水闸工程" value="sluice" />
              <el-option label="泵站工程" value="pumping" />
              <el-option label="其他工程" value="other" />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="工程规模" prop="projectScale">
            <el-select v-model="form.projectScale" placeholder="选择工程规模" style="width: 100%">
              <el-option label="大型" value="large" />
              <el-option label="中型" value="medium" />
              <el-option label="小型" value="small" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="建设状态" prop="constructionStatus">
            <el-select v-model="form.constructionStatus" placeholder="选择建设状态" style="width: 100%">
              <el-option label="在建" value="under_construction" />
              <el-option label="已建成" value="completed" />
              <el-option label="规划中" value="planning" />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="20">
        <el-col :span="24">
          <el-form-item label="项目地址" prop="projectAddress">
            <el-input v-model="form.projectAddress" placeholder="请输入项目详细地址" />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="总投资" prop="totalInvestment">
            <el-input v-model="form.totalInvestment" placeholder="请输入总投资">
              <template #append>万元</template>
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="建设工期" prop="constructionPeriod">
            <el-input v-model="form.constructionPeriod" placeholder="请输入建设工期">
              <template #append>月</template>
            </el-input>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="20">
        <el-col :span="24">
          <el-form-item label="项目描述" prop="projectDescription">
            <el-input
              v-model="form.projectDescription"
              type="textarea"
              :rows="4"
              placeholder="请输入项目描述"
            />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="20">
        <el-col :span="24">
          <el-form-item label="附件材料" prop="attachments">
            <el-upload
              class="upload-demo"
              drag
              action="#"
              multiple
              :file-list="form.attachments"
              :on-change="handleFileChange"
              :auto-upload="false"
            >
              <el-icon class="el-icon--upload"><upload-filled /></el-icon>
              <div class="el-upload__text">
                将文件拖到此处，或<em>点击上传</em>
              </div>
              <template #tip>
                <div class="el-upload__tip">
                  支持 jpg/png/pdf 文件，且不超过 10MB
                </div>
              </template>
            </el-upload>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="20">
        <el-col :span="24">
          <el-form-item label="备注" prop="remark">
            <el-input
              v-model="form.remark"
              type="textarea"
              :rows="3"
              placeholder="请输入备注信息"
            />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <template #footer>
      <div class="dialog-footer">
        <el-button @click="emit('update:modelValue', false)">取消</el-button>
        <el-button @click="resetForm">重置</el-button>
        <el-button type="primary" @click="handleSubmit">确定</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import { ref, reactive, watch } from 'vue'
import { ElMessage, type FormInstance, type FormRules } from 'element-plus'
import { UploadFilled } from '@element-plus/icons-vue'

interface Props {
  modelValue: boolean
  registrationData?: any
}

interface Emits {
  (e: 'update:modelValue', value: boolean): void
  (e: 'success'): void
}

const props = withDefaults(defineProps<Props>(), {
  registrationData: null
})

const emit = defineEmits<Emits>()

const formRef = ref<FormInstance>()
const isEdit = ref(false)

const form = reactive({
  registrationNo: '',
  registrationDate: '',
  applicantUnit: '',
  contactPerson: '',
  contactPhone: '',
  registrationType: '',
  projectName: '',
  projectType: '',
  projectScale: '',
  constructionStatus: '',
  projectAddress: '',
  totalInvestment: '',
  constructionPeriod: '',
  projectDescription: '',
  attachments: [] as any[],
  remark: ''
})

const rules: FormRules = {
  registrationNo: [
    { required: true, message: '请输入登记编号', trigger: 'blur' }
  ],
  registrationDate: [
    { required: true, message: '请选择登记日期', trigger: 'change' }
  ],
  applicantUnit: [
    { required: true, message: '请输入申请单位', trigger: 'blur' }
  ],
  contactPerson: [
    { required: true, message: '请输入联系人', trigger: 'blur' }
  ],
  contactPhone: [
    { required: true, message: '请输入联系电话', trigger: 'blur' },
    { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号码', trigger: 'blur' }
  ],
  registrationType: [
    { required: true, message: '请选择登记类型', trigger: 'change' }
  ],
  projectName: [
    { required: true, message: '请输入项目名称', trigger: 'blur' }
  ],
  projectType: [
    { required: true, message: '请选择项目类型', trigger: 'change' }
  ]
}

// 重置表单
const resetForm = () => {
  Object.assign(form, {
    registrationNo: '',
    registrationDate: '',
    applicantUnit: '',
    contactPerson: '',
    contactPhone: '',
    registrationType: '',
    projectName: '',
    projectType: '',
    projectScale: '',
    constructionStatus: '',
    projectAddress: '',
    totalInvestment: '',
    constructionPeriod: '',
    projectDescription: '',
    attachments: [],
    remark: ''
  })
  formRef.value?.clearValidate()
}

// 文件上传处理
const handleFileChange = (file: any, fileList: any[]) => {
  form.attachments = fileList
}

// 提交表单
const handleSubmit = async () => {
  if (!formRef.value) return
  
  try {
    await formRef.value.validate()
    
    // 这里应该调用API保存数据
    console.log('提交表单数据:', form)
    
    ElMessage.success(isEdit.value ? '修改成功' : '添加成功')
    emit('success')
    emit('update:modelValue', false)
  } catch (error) {
    ElMessage.error('请检查表单填写是否正确')
  }
}

// 监听弹窗打开，初始化表单数据
watch(() => props.modelValue, (newVal) => {
  if (newVal) {
    if (props.registrationData) {
      isEdit.value = true
      Object.assign(form, props.registrationData)
    } else {
      isEdit.value = false
      resetForm()
    }
  }
}, { immediate: true })
</script>

<style scoped>
.dialog-footer {
  text-align: right;
}

.upload-demo {
  width: 100%;
}
</style>